from flask_restful import Resource

from restful.api__.session import  login_required_current_user_generate, uid_required_target_user_generate
from restful.model.delete import DeleteInstance
from restful.model.query import QueryOneInstance
from restful.model.tables import Follow
from restful.model.update import UpdateInstance
from restful.response import ResponseConflict, ResponseSuccess


class FollowOrCancel(Resource):
    @login_required_current_user_generate
    @uid_required_target_user_generate
    def post(self,target_user,current_user):
        follow = QueryOneInstance(Follow,{'uid':current_user.id,'uid1':target_user.id})

        if follow:
            return ResponseConflict("请勿重复关注")

        UpdateInstance(Follow(uid=current_user.id,uid1=target_user.id))
        return ResponseSuccess("已关注")

    @login_required_current_user_generate
    @uid_required_target_user_generate
    def delete(self,current_user,target_user):
        follow = QueryOneInstance(Follow,{'uid':current_user.id,'uid1':target_user.id})

        if not follow:
            return ResponseConflict("关注不存在")
        DeleteInstance(follow)
        return ResponseSuccess("已取消关注")

